package com.byt.user.model;


import com.byt.base.model.BaseModel;
import com.byt.common.bean.VersionInfo;
import com.byt.netapi.bean.ResponseBean;
import com.byt.netapi.boby.FormBodys;
import com.byt.netapi.exception.ExceptionHandle;
import com.byt.netapi.observer.BaseObserver;
import com.byt.user.api.UserApi;
import com.byt.user.bean.LoginBean;

import java.lang.ref.WeakReference;
import java.util.Map;

public class LoginModel extends BaseModel<VersionInfo> {
    private Map<String, Object> map = null;

    public LoginModel(Map<String, Object> map) {
        this.map = map;
    }

    @Override
    public void refresh() {

    }

    @Override
    protected void load() {
        UserApi.getInstance().getVersionInfo(new BaseObserver<ResponseBean<VersionInfo>>(this) {
            @Override
            public void onNext(ResponseBean<VersionInfo> versionInfoResponseBean) {
                loadSuccess(versionInfoResponseBean.getData());
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {

            }
        }, map);
    }

    public void onLogin(FormBodys formBodys){
        UserApi.getInstance().onLogin(new BaseObserver<ResponseBean<LoginBean>>(this) {
            @Override
            public void onNext(ResponseBean<LoginBean> loginBeanResponseBean) {
                synchronized (this) {
                    mUiHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            for (WeakReference<IBaseModelListener> weakListener : mWeakListenerArrayList) {
                                if (weakListener.get() instanceof ILoginLister) {
                                    ILoginLister listenerItem = (ILoginLister) weakListener.get();
                                    if (listenerItem != null) {
                                        listenerItem.onLogin(loginBeanResponseBean.getData());
                                    }
                                }
                            }
                        }
                    }, 0);
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                loadFail(e.getCause().getMessage());
            }
        }, formBodys);
    }

    /**
     * 验证码登录
     * @param formBodys
     */
    public void onLoginCode(FormBodys formBodys){
        UserApi.getInstance().onLoginCode(new BaseObserver<ResponseBean<LoginBean>>(this) {
            @Override
            public void onNext(ResponseBean<LoginBean> loginBeanResponseBean) {
                synchronized (this) {
                    mUiHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            for (WeakReference<IBaseModelListener> weakListener : mWeakListenerArrayList) {
                                if (weakListener.get() instanceof ILoginLister) {
                                    ILoginLister listenerItem = (ILoginLister) weakListener.get();
                                    if (listenerItem != null) {
                                        listenerItem.onLogin(loginBeanResponseBean.getData());
                                    }
                                }
                            }
                        }
                    }, 0);
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                   loadFail(e.getCause().getMessage());
            }
        }, formBodys);
    }


    /**
     * 获取验证码
     */
    public void getCode(FormBodys formBodys) {
        UserApi.getInstance().onGetCode(new BaseObserver<ResponseBean<String>>(this) {
            @Override
            public void onNext(ResponseBean<String> enity) {
                synchronized (this) {
                    mUiHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            for (WeakReference<IBaseModelListener> weakListener : mWeakListenerArrayList) {
                                if (weakListener.get() instanceof ILoginLister) {
                                    ILoginLister listenerItem = (ILoginLister) weakListener.get();
                                    if (listenerItem != null) {
                                        listenerItem.getCode(enity.getData(),enity.getMsg());
                                    }
                                }
                            }
                        }
                    }, 0);
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                  loadFail(e.getCause().getMessage());
            }
        }, formBodys);
    }

    public interface ILoginLister<T> extends IModelListener<T> {
       void onLogin(LoginBean loginBean);

       void getCode(String message,String code);
    }
}
